package com.sgb.kjwl.view.ui.adapter.home;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.view.MotionEventCompat;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.swgk.core.view.entity.EnterpriseRecEntity;
import com.swgk.core.customview.OnDragVHListener;
import com.swgk.core.customview.OnItemMoveListener;

import java.util.List;

/**
 * Description：首页企业设置可移动适配器
 * <p>
 * Created by LiWei on 2021/11/1
 */
public class DragCompanyConfigAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements OnItemMoveListener {

    // 我的频道
    public static final int TYPE_MY = 1;

    private static final long ANIM_TIME = 360L;

    // touch 点击开始时间
    private long startTime;
    // touch 间隔时间  用于分辨是否是 "点击"
    private static final long SPACE_TIME = 100;

    private LayoutInflater mInflater;
    private ItemTouchHelper mItemTouchHelper;


    private List<EnterpriseRecEntity> mMyEnterpriseRecItems;

    // 我的频道点击事件
    private OnClickItemListener listener;

    public DragCompanyConfigAdapter(Context context, ItemTouchHelper helper, List<EnterpriseRecEntity> mMyEnterpriseRecItems) {
        this.mInflater = LayoutInflater.from(context);
        this.mItemTouchHelper = helper;
        this.mMyEnterpriseRecItems = mMyEnterpriseRecItems;
    }

    @Override
    public int getItemViewType(int position) {
        return TYPE_MY;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        final View view;
        switch (viewType) {
            case TYPE_MY:
                view = mInflater.inflate(R.layout.item_com_config_drag, parent, false);
                final MyViewHolder myHolder = new MyViewHolder(view);

                myHolder.textView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(final View v) {
                        mItemTouchHelper.startDrag(myHolder);
                        return true;
                    }
                });

                myHolder.textView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        switch (MotionEventCompat.getActionMasked(event)) {
                            case MotionEvent.ACTION_DOWN:
                                startTime = System.currentTimeMillis();
                                break;
                            case MotionEvent.ACTION_MOVE:
                                if (System.currentTimeMillis() - startTime > SPACE_TIME) {
                                    mItemTouchHelper.startDrag(myHolder);
                                }
                                break;
                            case MotionEvent.ACTION_CANCEL:
                            case MotionEvent.ACTION_UP:
                                startTime = 0;
                                break;
                        }
                        return false;
                    }
                });
                return myHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            String titleName = mMyEnterpriseRecItems.get(position).getCompanyName();
            MyViewHolder myHolder = (MyViewHolder) holder;
            if (!TextUtils.isEmpty(titleName)) {
                if (titleName.length() <= 6) {
                    myHolder.textView.setText(titleName);
                } else {
                    myHolder.textView.setText(titleName.substring(0, 6) + "...");
                }
            }
            myHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mMyEnterpriseRecItems.remove(position);
                    listener.onDelete(mMyEnterpriseRecItems);
                }
            });
            myHolder.textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mMyEnterpriseRecItems.remove(position);
                    listener.onDelete(mMyEnterpriseRecItems);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mMyEnterpriseRecItems.size();
    }

    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        EnterpriseRecEntity item = mMyEnterpriseRecItems.get(fromPosition);
        mMyEnterpriseRecItems.remove(fromPosition);
        mMyEnterpriseRecItems.add(toPosition, item);
        notifyItemMoved(fromPosition, toPosition);
    }

    public void setListener(OnClickItemListener listener) {
        this.listener = listener;
    }


    public interface OnClickItemListener{

        void onDelete(List<EnterpriseRecEntity> editList);

        void onFinish(List<EnterpriseRecEntity> editList);
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements OnDragVHListener {
        private TextView textView;
        private ImageView imgEdit;

        public MyViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.tv_content);
            imgEdit = (ImageView) itemView.findViewById(R.id.img_edit);
        }

        /**
         * item 被选中时
         */
        @Override
        public void onItemSelected() {
            textView.setBackgroundResource(R.drawable.bg_channel_p);
        }

        /**
         * item 取消选中时
         */
        @Override
        public void onItemFinish() {
            textView.setBackgroundResource(R.drawable.bg_channel);
        }
    }

    public void setData(List<EnterpriseRecEntity> editList){
        this.mMyEnterpriseRecItems = editList;
        notifyDataSetChanged();
    }

    public void closeData(){
        this.mMyEnterpriseRecItems.clear();
        notifyDataSetChanged();
    }
}
